package chatroom_tests;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ChatroomCases extends InitAndEnd{
    public static Stream<Arguments> getFriendTitle() {
        return  Stream.of(Arguments.arguments("wangwu"));
    }

    //登录成功测试案例
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "login_success.csv")
    void loginSuccess(String username,String password,String exp_url) {
        //打开登录页面
        webDriver.get("http://127.0.0.1:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入用户名
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        //输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //弹出登录成功弹窗
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到会话页面
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(exp_url,cur_url);
        //会话页面的username名字与登录相同则登录成功

        String cur_name = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username, cur_name);

    }
    //点击会话列表测试案例
    @Order(2)
    @ParameterizedTest
    @MethodSource("getFriendTitle")
    void sessionList(String exp_friendTitle){
        // 点击会话标签
        webDriver.findElement(By.cssSelector(".tab-session")).click();
        //点击已有的想要聊天的会话
        webDriver.findElement(By.cssSelector("#session-list > li:nth-child(1)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取右侧标题好友名字
        String cur_friendTitle = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //若右侧好友名与预期值对应则成功
        Assertions.assertEquals(exp_friendTitle, cur_friendTitle);
    }

    //编辑消息及发送消息
    @Test
    @Order(3)
    void edit_message(){
        //点击消息框
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("111");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击发送
    }
}
